package cn.itsource.pethome.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

/**
 * 发邮件的类
 */
@Component
public class EmailUtil {
    @Resource
    private JavaMailSender javaMailSender;

    /**
     *
     * @param toEmail 收件人邮箱
     * @param note 驳回入驻申请原因
     */
    public  void  easySend(String toEmail,String note){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1137358239@qq.com");
        //邮件主题
        mailMessage.setSubject("宠物之家信息");
        //邮件内容  后期可加上管理员联系方式
        mailMessage.setText("宠物之家驳回入驻申请。可能原因如下："+note);
        //收件人
        mailMessage.setTo(toEmail);
        //发送邮件
        javaMailSender.send(mailMessage);
    }

    /**
     *
     * @param toEmail 收件人邮箱
     * @param id 数据id
     * @param b true代表用户，false代表店铺管理员
     * @throws Exception
     */
    public void complexSend(String toEmail,Long id,Boolean b) throws Exception{
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1137358239@qq.com");
        helper.setSubject("宠物之家激活认证");
        //第二个参数 true 代表是否是一个html内容
        if(!b)
            helper.setText("<h1>点击下方链接即可激活账号</h1>"+
              "<a href='http://localhost:8080/shop/shopActivation/"+id+"' >宠物之家激活链接</a>",true);
        else
            helper.setText("<h1>点击下方链接即可激活账号</h1>"+
              "<a href='http://localhost:8080/user/activation/"+id+"' >宠物之家激活链接</a>",true);
        //添加附件
//        helper.addAttachment("文件名.jpg",new File("C:\\aa.jpg"));
//        helper.addAttachment("压缩文件", new File("C:\\resources.zip"));
        //收件人
        helper.setTo(toEmail);
        //发送邮件
        javaMailSender.send(mimeMessage);
    }
}